<template>
  <el-form size="small" label-width="83px" class="box-form" label-position="right">
    <template v-for="(item, index) in props.queryList" :key="index">
      <el-form-item :label="`${item.options.label}`">
        <el-input v-if="item.type==='text'" :maxlength="item.maxlength || 50" :placeholder="item.options.placeholder" v-model="params[item.field]" clearable></el-input>
        <el-select v-if="item.type==='select'" :placeholder="item.options.placeholder" v-model="params[item.field]" clearable>
          <el-option v-for="(data, dataIndex) in confAll[item.optionsKey]" :key="`${index}-${dataIndex}`" :label="data[item.labelKey] || data['label']" :value="data[item.valueKey] || data['key'] || 0"></el-option>
        </el-select>
        <el-date-picker v-if="item.type==='dateRange'" v-model="params[item.field]" type="daterange" clearable unlink-panels range-separator="至" :start-placeholder="item.options.placeholder[0] || '开始日期'" :end-placeholder="item.options.placeholder[1] || '结束日期'"></el-date-picker>
      </el-form-item>
    </template>
    <el-form-item v-if="props.queryList && ([0, 2].includes(props.queryList.length % 4))"></el-form-item>
    <el-form-item class="btns">
      <el-button size="small" type="primary" @click="query">查询</el-button>
      <el-button size="small" @click="reset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { useStore } from 'vuex'
import { defineComponent, reactive, computed } from 'vue'
import dayjs from 'dayjs'

export default defineComponent({
  name: 'queryModel',
  props: {
    queryList: [Array]
  },
  emits: ['changeParams'],
  setup (props, context) {
    // 获取 store 中的配置
    const store = useStore()
    const confAll = computed(() => {
      return store.state.confAll
    })

    // 表单数据
    let params = reactive({})
    const query = () => {
      let newParams = Object.assign({}, params)
      newParams.pageIndex = 1
      for (let key in newParams) {
        if (key.indexOf(',') > 0) {
          newParams[key] = newParams[key] || []
          let keys = key.split(',')
          keys.forEach((item, index) => {
            newParams[keys[index]] = newParams[key][index] ? dayjs(newParams[key][index]).format('YYYY-MM-DD') : ''
          })
          delete newParams[key]
        }
      }
      context.emit('changeParams', newParams)
    }
    const reset = () => {
      Object.keys(params).forEach(o => {
        params[o] = ''
      })
      query()
    }

    return {
      props,
      params,
      confAll,
      query,
      reset
    }
  }
})

</script>

<style scoped lang="less">
  .box-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 0 40px;
    .el-form-item {
      width: 24%;
      display: flex;
      &:deep(.el-form-item__label) {
        color: #222222;
      }
      &:deep(.el-form-item__content) {
        flex: 1;
        margin-left: 0!important;
      }
      .el-input, .el-select, .el-cascader, .text, .el-autocomplete {
        width: 100%;
        &:deep(input) {
          border-radius: 2px;
        }
        &:deep(.el-input-group__append) {
          border-radius: 2px;
          padding: 0 10px !important;
        }
        &:deep(.el-input__inner) {
          color: #222222;
          &:hover {
            border-color: #409EFF;
          }
        }
      }
      &:deep(.el-range-input) {
        color: #222222 !important;
      }
      &:deep(.el-date-editor) {
        border-radius: 2px;
        width: 100%;
        &:hover {
          border-color: #409EFF;
        }
      }
    }
  }

  .btns {
    text-align: right;
    .el-button {
      border-radius: 4px;
      width: 70px;
    }
  }
</style>
